//
//  BSTabBarController.m
//  BaiSi
//
//  Created by Sycamore on 16/4/27.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSNavigationController.h"
#import "BSTabBarController.h"
#import "BSEssenceViewController.h"
#import "BSNewViewController.h"
#import "BSPublishViewController.h"
#import "BSFriendTrendViewController.h"
#import "BSMeViewController.h"
#import "BSTabBar.h"

@interface BSTabBarController () <UITabBarControllerDelegate>

@property (nonatomic,strong) UIViewController *selectedVc;

@end

@implementation BSTabBarController

//选中文字被渲染，且要改变文字大小，就不能直接通过tintColor修改了
+ (void)load
{
    //拿到全局的item进行设置
    UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedIn:self, nil];
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    attr[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    //注意，设置nomal才能改变文字大小
    [tabBarItem setTitleTextAttributes:attr forState:UIControlStateNormal];
    
    NSMutableDictionary *attri = [NSMutableDictionary dictionary];
    attri[NSForegroundColorAttributeName] = [UIColor blackColor];
    [tabBarItem setTitleTextAttributes:attri forState:UIControlStateSelected];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建自定义tabBar
    BSTabBar *diyTabBar = [[BSTabBar alloc]init];
    diyTabBar.operation =  ^{
    
        //发布
        BSPublishViewController *publish = [[BSPublishViewController alloc]init];
//        publish.view.backgroundColor = [UIColor whiteColor];
        
        //不让其有动画，注意，设置不动画的时候，会对transitionDelegate的动画方法产生影响，导致不生效
        [self presentViewController:publish animated:NO completion:nil];
    };
    //只读属性可以使用KVC进行赋值,用自定义的tabBar覆盖掉系统tabBar的时候，不用设置frame
    
    //注意：更换系统的tabBar,tabBar上的barButton还是会加上来的，一般是在viewWillAppear的时候加上来，这是系统做的事情
    [self setValue:diyTabBar forKey:@"tabBar"];
    
    //创建子控制器
    [self addAllChildViewControllers];
    
    //设置代理
    self.delegate = self;
    
    //选址选中的控制器
    _selectedVc = [self.childViewControllers firstObject];
    
    
}


- (void)addAllChildViewControllers
{
    //精华
    BSEssenceViewController *essence = [[BSEssenceViewController alloc]init];
    [self addOneChildController:essence title:@"精华" imageName:@"tabBar_essence_icon" selectedImageName:@"tabBar_essence_click_icon"];
    
    
    //新帖
    BSNewViewController *new = [[BSNewViewController alloc]init];
    [self addOneChildController:new title:@"新帖" imageName:@"tabBar_new_icon" selectedImageName:@"tabBar_new_click_icon"];
    
    
    //关注
    BSFriendTrendViewController *friend = [[BSFriendTrendViewController alloc]init];
    [self addOneChildController:friend title:@"关注" imageName:@"tabBar_friendTrends_icon" selectedImageName:@"tabBar_friendTrends_click_icon"];
    
    
    //我
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"BSMeViewController" bundle:nil];
    BSMeViewController *me = [storyBoard instantiateInitialViewController];
    [self addOneChildController:me title:@"我" imageName:@"tabBar_me_icon" selectedImageName:@"tabBar_me_click_icon"];
}



//添加一个子控制器
- (void)addOneChildController:(UIViewController *)viewController title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    UIImage *image = [UIImage OriginalRenderImageWithImageName:imageName];
    UIImage *selImage = [UIImage OriginalRenderImageWithImageName:selectedImageName];
    
    viewController.tabBarItem.title = title;
    viewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -1.0f);
    viewController.tabBarItem.image = image;
    viewController.tabBarItem.selectedImage = selImage;
    
    BSNavigationController *navi = [[BSNavigationController alloc]initWithRootViewController:viewController];
    [self addChildViewController:navi];
}

#pragma mark UITabBarControllerDelegate

//选中某个子控制器的时候就会调用此方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (_selectedVc == viewController) {
        //发送通知，让正显示在屏幕上的tableView去实现下拉刷新
        [[NSNotificationCenter defaultCenter]postNotificationName:BSTableRefreshNote object:nil];
    }
    //记录新的选中控制器
    _selectedVc = viewController;
}
@end
